/**
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *   
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 */
package org.jiopi.ibean.kernel.repository;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.jiopi.ibean.kernel.NameVersion;
import org.jiopi.ibean.kernel.config.BlueprintAnnotations;
import org.jiopi.ibean.kernel.config.ConfigConstants;
import org.jiopi.ibean.kernel.config.ModuleAnnotations;
import org.jiopi.ibean.kernel.config.ModuleJar;
import org.jiopi.ibean.kernel.util.AnnotationParser;


/**
 * 
 * 模块资源
 * 
 * 用于描述一个 module 的资源列表
 * 
 * @since 2010.4.19
 *
 */
public class ModuleResource extends NameVersion{
	
	private static Logger logger = Logger.getLogger(ModuleResource.class);
	
	
	
	/**
	 * resources of this module or blueprint
	 */
	public final ModuleJar moduleResource;
	
	/**
	 * resources of dependent blueprints
	 */
	public final NameVersion[] dependentBlueprintResources;
	
	/**
	 * other resources of this module
	 */
	public final URL[] localDependentJars;
	
	
	/**
     * Auto parsing the specified module's URL。 
     * @param nameVersion   the name version of the module
     * @param resources     module's resources
     * @param resourceType   Repository.BLUEPRINT or  Repository.MODULE
     */
	public ModuleResource(NameVersion nameVersion,URL[] resources,int resourceType){
		super(nameVersion);
		ArrayList<ModuleJar> moduleResourceList = new ArrayList<ModuleJar>();
		ArrayList<NameVersion> dependentBlueprintResourceList = new ArrayList<NameVersion>();
		ArrayList<URL> localDependentJarList = new ArrayList<URL>();
		parseResources(resources,resourceType,moduleResourceList,dependentBlueprintResourceList,localDependentJarList);
		if(moduleResourceList.size()>0){
			moduleResource = moduleResourceList.get(0);
		}else{
			moduleResource = null;
		}
		
		dependentBlueprintResources = dependentBlueprintResourceList.toArray(new NameVersion[dependentBlueprintResourceList.size()]);
		localDependentJars = localDependentJarList.toArray(new URL[localDependentJarList.size()]);
		
	}
	
	private void parseResources(URL[] resources,int resourceType,ArrayList<ModuleJar> moduleResourceList,ArrayList<NameVersion> dependentBlueprintResourceList,ArrayList<URL> localDependentJarList){
		
		for(URL resource:resources){
			JarInfo ji = new JarInfo(resource);
			if(ji.jarType!=-1){
				if(resourceType == Repository.BLUEPRINT){
					if(ji.jarType == ConfigConstants.BLUEPRINT_JAR){
						if(this.name.endsWith(ji.resourceName)){
							BlueprintAnnotations anno = Repository.getBlueprintAnnotations(resource);
							if(anno!=null){
								moduleResourceList.add(new ModuleJar(resource,anno));
							}
							else{
								logger.warn("parse "+resource +" as a blueprint error!");
							}
						}else{
							dependentBlueprintResourceList.add(new NameVersion(ji.resourceName,ji.resourceVersion));
						}
					}else{
						localDependentJarList.add(resource);
					}
				}else if(resourceType == Repository.MODULE){
					if(ji.jarType == ConfigConstants.BLUEPRINT_JAR){
						dependentBlueprintResourceList.add(new NameVersion(ji.resourceName,ji.resourceVersion));
					}else if(ji.jarType == ConfigConstants.MODULE_JAR){
						ModuleAnnotations anno = Repository.getModuleAnnotations(resource);
						if(anno!=null){
							moduleResourceList.add(new ModuleJar(resource,anno));
						}else{
							logger.warn(resource +" is not a correct module jar");
						}
					}else{
						localDependentJarList.add(resource);
					}
				}
			}else{
				File resourceFile = new File(resource.getFile());
				int jarType = AnnotationParser.parseJarFile(resourceFile, null, false, false);
				if(resourceType == Repository.BLUEPRINT){
					if(jarType == ConfigConstants.BLUEPRINT_JAR){
						BlueprintAnnotations anno = Repository.getBlueprintAnnotations(resource);
						if(anno!=null){
							String blueprintName = anno.getName();
							if(this.name.endsWith(blueprintName)){
								moduleResourceList.add(new ModuleJar(resource,anno));
							}else{
								dependentBlueprintResourceList.add(new NameVersion(anno.getName(),anno.getVersion()));
							}
						}else{
							logger.warn("parse "+resource +" as a blueprint error!");
						}
					}else{
						localDependentJarList.add(resource);
					}
				}else if(resourceType == Repository.MODULE){
					if(jarType == ConfigConstants.BLUEPRINT_JAR){
						BlueprintAnnotations anno = Repository.getBlueprintAnnotations(resource);
						if(anno!=null){
							dependentBlueprintResourceList.add(new NameVersion(anno.getName(),anno.getVersion()));
						}else{
							logger.warn("parse "+resource +" as a blueprint error!");
						}
					}else if(jarType == ConfigConstants.MODULE_JAR){
						ModuleAnnotations anno = Repository.getModuleAnnotations(resource);
						if(anno!=null){
							moduleResourceList.add(new ModuleJar(resource,anno));
						}else{
							logger.warn(resource +" is not a correct module jar");
						}
					}else{
						localDependentJarList.add(resource);
					}
				}
			}
		}
	}
	
	public static class JarInfo{
		public final int jarType;
		public final String resourceName;
		public final String resourceVersion;
		
		public JarInfo(File jarFile){
			this(jarFile.getName());
		}
		
		public JarInfo(URL jarURL){
			this(new File(jarURL.getFile()).getName());
		}
		
		public JarInfo(String fileName){
			int type = -1;
			String name = null;
			String v = null;
			
			if(fileName.startsWith("blueprint-")){
				type = ConfigConstants.BLUEPRINT_JAR;
			}else if(fileName.startsWith("module-")){
				type = ConfigConstants.MODULE_JAR;
			}
			if(type!=-1){
				int beginPos = fileName.indexOf('-');
				int endPos = fileName.lastIndexOf('-');
				if(endPos>beginPos){
					name = fileName.substring(beginPos+1,endPos);
					v = fileName.substring(endPos+1,fileName.length()-4);
				}else{
					type = -1;
				}
			}
			
			jarType = type;
			resourceName = name;
			resourceVersion = v;
		}
	}
}
